/**************************************************************************************** 
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OptionalAdditionalOfferModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneProductModuleVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdFeeCache;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;

/** 
 * <Description> <br> 
 *  
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2022年3月14日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module <br>
 */
@Service
public class PreCancellationAdditionalOfferModuleHandler extends AbstractModuleHandler {

    @Autowired
    IQuerySceneInstService querySceneInstService;
    
    @Autowired
    private OptionalAdditionalOfferModuleHandler handler;
    
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    
    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
//        String sceneInstId = context.getSceneVo().getSceneInstId();
//        operateSceneInstService.putExtParameter(sceneInstId, "PAY_DETAIL_FLAG", KeyConsts.IFTRUE_T);
//        Map<String, OptionalAdditionalOfferModuleVO> additionalOfferModuleVO = handler.getOptionalAdditionalOfferModuleVO(sceneInstId, false);
//
//        SceneProductModuleVO sceneFeeInfoVo = ((SceneNewOfferSeneVO) context.getSceneVo()).getSceneProductModuleVO();
        List<OptionalAdditionalOfferModuleVO> additionalOfferModuleVOList = new ArrayList<>();

//        for (Map.Entry<String, OptionalAdditionalOfferModuleVO> entry : additionalOfferModuleVO.entrySet()) {
//            if (KiteMapUtils.isNotEmpty(entry.getValue().getTableFeeAttr())) {
//                entry.getValue().setAddButton(false);
//                additionalOfferModuleVOList.add(entry.getValue());
//            }
//        }
//        if (KiteListUtils.isNotEmpty(additionalOfferModuleVOList)) {
//            sceneFeeInfoVo.setAdditionalOffers(additionalOfferModuleVO);
//        }
        return additionalOfferModuleVOList;
    }
    
    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {

        Map<String, OptionalAdditionalOfferModuleVO> additionalOfferModuleVOMap = new HashMap<>();
        List<OptionalAdditionalOfferModuleVO> additionalOfferModuleVOList = new ArrayList<>();
        //所有的主套餐
//        String sceneInstId = context.getAcceptRequest().getSceneInstId();
//        List<KiteOfferInst> offerInsts = querySceneInstService.listMainOfferInsts(sceneInstId);
//        for (KiteOfferInst kiteOfferInst : offerInsts) {
//
//            List<KiteOfferInst> additionalOfferInsts = querySceneInstService.listAdditionalOfferInstsFromOfferInst(KiteWebContext.getContext().getCustId(), kiteOfferInst.getOfferInstId());
//            if (KiteListUtils.isNotEmpty(additionalOfferInsts)) {
//                OptionalAdditionalOfferModuleVO additionalOfferModuleVO = new OptionalAdditionalOfferModuleVO();
//
//                List<KiteOfferInst> additionalOfferInsts1 = additionalOfferInsts.stream().filter(offerInst -> {
//                    Map<String, Object> offerMap = ProdFeeCache.get(offerInst.getOfferId()).getOfferMap();
//                    String payType = KiteMapUtils.getString(offerMap, "payType");
//                    return KiteStringUtils.equalsAny(payType, LocalKeyConsts.FEE_PAY_TYPE_ONE_OFF);
//                }).collect(Collectors.toList());
//                if (KiteListUtils.isEmpty(additionalOfferInsts1)) {
//                    continue;
//                }
//                handler.DealAdditionalOfferInst(sceneInstId, kiteOfferInst, additionalOfferModuleVO, additionalOfferInsts1, true, null, "1");
//                
//                additionalOfferModuleVO.setOfferInstId(kiteOfferInst.getOfferInstId());
//                additionalOfferModuleVO.setOfferName(OfferCache.get(kiteOfferInst.getOfferId()).getOfferName());
//                additionalOfferModuleVO.setOfferInstId(kiteOfferInst.getOfferInstId());
//                additionalOfferModuleVO.setPreviewFlag(true);
//                additionalOfferModuleVOMap.put(kiteOfferInst.getOfferInstId(), additionalOfferModuleVO);
//                additionalOfferModuleVOList.add(additionalOfferModuleVO);
//            }
//
//        }
//        SceneProductModuleVO sceneFeeInfoVo = ((SceneNewOfferSeneVO) context.getSceneVo()).getSceneProductModuleVO();
//        sceneFeeInfoVo.setAdditionalOffers(additionalOfferModuleVOMap);
        return additionalOfferModuleVOList;
    }
}